﻿using Minotaur.App.Domains.Dto;
using Minotaur.App.Domains.Tenement;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minotaur.App.Core
{
    public class DBContext : IDbContext
    {
        SqlSugarScope _masterDbContext;
        SqlSugarClient? sqlSugarClient;
        public DBContext(SqlSugarScope masterDbContext)
        {
            _masterDbContext = masterDbContext;
            if (GlobalUserModel != null)
                GetBusinessDb();
        }

        private void GetBusinessDb()
        {
            if (!string.IsNullOrEmpty(GlobalUserModel?.TenementCode))
                sqlSugarClient = GetCommonDb(GlobalUserModel.TenementCode);
        }
        private SqlSugarClient GetCommonDb(string TenementCode)
        {
            TenantInformation tenantInformation = this.MasterDb.Queryable<TenantInformation>().Single(o => o.Code == TenementCode);
            if (null != tenantInformation)
            {
                var sqlSugarClients = new SqlSugarClient(new List<ConnectionConfig>()
                {
                    new ConnectionConfig()
                    {
                        ConfigId=GlobalConst.GolbalBusinessDataBaseTag,
                        IsAutoCloseConnection=true,
                        ConnectionString=tenantInformation.DBConnection,
                        DbType=(DbType)tenantInformation.DataBaseType
                    },
                    new ConnectionConfig()
                    {
                        ConfigId=GlobalConst.GolbalWFDataBaseTag,
                        IsAutoCloseConnection=true,
                        ConnectionString=tenantInformation.WF_DBConnection,
                        DbType=(DbType)tenantInformation.DataBaseType
                    },new ConnectionConfig()
                    {
                        ConfigId=GlobalConst.GolbalFormDataBaseTag,
                        IsAutoCloseConnection=true,
                        ConnectionString=tenantInformation.Form_DBconnection,
                        DbType=(DbType)tenantInformation.DataBaseType
                    },
                    new ConnectionConfig()
                    {
                        ConfigId=GlobalConst.GolbalFileDataBaseTag,
                        IsAutoCloseConnection=true,
                        ConnectionString=tenantInformation.Files_DBconnection,
                        DbType=(DbType)tenantInformation.DataBaseType
                    }
                });
                return sqlSugarClients;
            }
            return null;
        }
        /// <summary>
        /// 平台库
        /// </summary>
        public ISqlSugarClient MasterDb => _masterDbContext;

        /// <summary>
        /// 业务库
        /// </summary>
        public ISqlSugarClient BusinessDb => sqlSugarClient.GetConnection(GlobalConst.GolbalBusinessDataBaseTag);

        /// <summary>
        /// 文件库
        /// </summary>
        public ISqlSugarClient FileDb => sqlSugarClient.GetConnection(GlobalConst.GolbalFileDataBaseTag);

        /// <summary>
        /// 表单库
        /// </summary>
        public ISqlSugarClient FormDb => sqlSugarClient.GetConnection(GlobalConst.GolbalFormDataBaseTag);

        /// <summary>
        /// 流程库
        /// </summary>
        public ISqlSugarClient FlowDb => sqlSugarClient.GetConnection(GlobalConst.GolbalWFDataBaseTag);

        public GlobalUserModel GlobalUserModel { get; set; }


        public SqlSugarClient SqlSugarClient => sqlSugarClient;

        public ISqlSugarClient GetDB(string tenantCode, string dataBaseType)
        {

            var res = GetCommonDb(tenantCode);
            sqlSugarClient = res;
            if (null != res)
            {
                return res.GetConnection(dataBaseType);
            }
            return null;
        } 
    }
}
